

module MainCover where

import Data.Graph.Inductive.Example(labUEdges)
import Data.Graph.Inductive.Tree(UGr)
import Data.Graph.Inductive.Graph(mkGraph,nodes)

import Cover
import DotCover

import System(getArgs, getProgName)
import System.Console.GetOpt
import Data.Maybe(fromMaybe)

import Data.Time.Clock.POSIX

findCover input output = do
  putStrLn "Reading input file..."
  file <- readFile input
  let graph = readDot file
  putStrLn "Determine number of populations. For most of graphs 3 is enough"
  numberOfPopulations <- readLn
  putStrLn "Start cover finding..."
  start <- graph `seq` getPOSIXTime
  (cover, usefullNum) <- geneticAlgorithm graph numberOfPopulations
  end <- usefullNum `seq` getPOSIXTime
  putStrLn $ "Exec time: " ++ show (end - start)
  putStrLn $ "Size of founded cover: " ++ (show $ length cover)
  putStrLn $ "Number of usefull populations: " ++ (show usefullNum)
  putStrLn $ "Number of vertecies in graph: " ++ (show $ length $ nodes graph)
  writeFile output $ showGraph graph cover

main = do
  args <- getArgs
  progName <- getProgName
  case getOpt RequireOrder options args of
    ([Input i , Output o], [], []) -> findCover i o
    ([Output o , Input i], [], []) -> findCover i o
    (_, [], []) ->
      error $ "-i and -o are required!\n" ++ usageInfo (header progName) options
    (_, nonOpts, []) ->
      error $ "unrecognized arguments: " ++ unwords nonOpts
    (_, _, msgs) ->
      error $ concat msgs ++ usageInfo (header progName) options

data Flag = Input String | Output String

options :: [OptDescr Flag]
options = [
    Option ['i'] ["input"] (ReqArg Input "FILE") "input file",
    Option ['o'] ["output"] (ReqArg Output "FILE") "output file"
  ]

header progName = "Usage: " ++ progName ++ " -i input.dot -o output.dot"
